/* eslint valid-jsdoc: "off" */

'use strict'
const path = require('path')
const CryptoJS = require('crypto-js')

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
	/**
	 * built-in config
	 * @type {Egg.EggAppConfig}
	 **/
	const config = (exports = {})

	// use for cookie sign key, should change to your own and keep security
	config.keys = appInfo.name + '_jjskdof9123_dfs0a8sd'

	// add your middleware config here
	config.middleware = ['tokenHandler']
	// jwt
	config.tokenHandler = {
		match(ctx) {
			const url = ctx.request.url
			if (
				url.startsWith('/admin/login') ||
				url.startsWith('/admin/captcha') ||
				url.startsWith('/admin/register') ||
				url.startsWith('/swagger') ||
				url.startsWith('/favicon') ||
				url.startsWith('/public') ||
				url.startsWith('/api')
			) {
				return false // close jwt
			} else {
				return true // open jwt
			}
		},
	}
	// add your user config here
	const userConfig = {
		// myAppName: 'egg',
	}
	// close csrf
	config.security = {
		csrf: {
			enable: false,
		},
	}

	config.sequelize = {
		dialect: 'mysql',
		host: 'localhost',
		port: 3306,
		database: 'blog',
		username: 'root',
		password: 'ninja427Yq',
	}
	// config.sequelize = {
	// 	dialect: 'mysql',
	// 	host: '101.37.30.31',
	// 	port: 3306,
	// 	database: 'blog',
	// 	username: 'root',
	// 	password: 'ninja427Yq',
	// }
	// config.sequelize = {
	// 	dialect: 'mysql',
	// 	host: 'localhost',
	// 	port: 3306,
	// 	database: 'blog-dev',
	// 	username: 'root',
	// 	password: '123456',
	// }

	config.swaggerdoc = {
		dirScanner: './app/controller',
		apiInfo: {
			title: 'blog-admin',
			description: 'blog-admin',
			version: '1.0.0',
		},
		schemes: ['http', 'https'],
		consumes: ['application/json'],
		produces: ['application/json'],
		enableSecurity: true,
		securityDefinitions: {
			apikey: {
				type: 'apiKey',
				name: 'authorization',
				in: 'header',
			},
		},
		routerMap: true,
		enable: true,
	}

	config.static = {
		prefix: '/public',
		dir: path.join(appInfo.baseDir, 'app/public'), // `String` or `Array:[dir1, dir2, ...]` 静态化目录,可以设置多个静态化目录
		dynamic: true, // 如果当前访问的静态资源没有缓存，则缓存静态文件，和`preload`配合使用；
		preload: false,
		maxAge: 31536000, // in prod env, 0 in other envs
		buffer: true, // in prod env, false in other envs
	}
	config.KEY = CryptoJS.enc.Utf8.parse('sdkfopi90831290ikdas')
	config.IV = CryptoJS.enc.Utf8.parse('sdfi90213890dfuajiosdf')
	config.secret = '128903jdfoas9312jo9dffdfsdf'
	return {
		...config,
		...userConfig,
	}
}
